package com.jinlejia.erp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jinlejia.erp.R;
import com.jinlejia.erp.utils.DateUtil;
import com.jinlejia.erp.utils.StringUtil;
import com.jinlejia.erp.widget.musicView.MusicView;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/18 0018.
 */

public class HouseFollowListAdapter extends BaseAdapter {
    private ArrayList<Map<String, Object>> dataList;
    private View cacheView;
    private String cur_play_url;

    public HouseFollowListAdapter(ArrayList<Map<String, Object>> list) {
        this.dataList = list;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if ((dataList.get(position).containsKey("followStyle") && dataList.get(position).get("followStyle").toString().equals("录音跟进"))) {
            if( null != cacheView && cur_play_url.equals( dataList.get(position).get("followContent").toString() )  ){
                convertView = cacheView;
            }else {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow_voice_layout, parent, false);
                final MusicView musicView = (MusicView) convertView.findViewById(R.id.music_view);
                ImageView protect = (ImageView) convertView.findViewById(R.id.protect);
                final View finalConvertView = convertView;

                //录音保护
                if ( dataList.get(position).containsKey("followState") && dataList.get(position).get("followState").equals("4")) {
                    protect.setVisibility(View.VISIBLE);
                    musicView.setVisibility(View.GONE);
                }else{
                    musicView.setVisibility(View.VISIBLE);
                    protect.setVisibility(View.GONE);
                }

                musicView.setOnControlClickListener(new MusicView.OnControlClickListener() {
                    @Override
                    public void onClick() {
                        cacheView = finalConvertView;
                        cur_play_url = dataList.get(position).get("followContent").toString();
                        notifyDataSetChanged();
                        musicView.setPlayUrl(dataList.get(position).get("followContent").toString());
                        musicView.controlBtnClick();
                    }
                });

                ((TextView) convertView.findViewById(R.id.phone_no)).setText(dataList.get(position).get("callingId").toString());

                convertView.findViewById(R.id.call_icon).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != callListener) {
                            callListener.onCall(dataList.get(position).get("callingId").toString());
                        }
                    }
                });
            }

        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow_info_layout, parent, false);
            ((TextView) convertView.findViewById(R.id.info)).setText(dataList.get(position).get("followContent").toString());
        }
        //时间
        ((TextView) convertView.findViewById(R.id.follow_time)).setText(DateUtil.getTime(dataList.get(position).get("followTime").toString(), "yyyy-MM-dd HH:mm"));
        // 经纪人信息
        if (dataList.get(position).containsKey("userName"))
            ((TextView) convertView.findViewById(R.id.broker_info)).setText(dataList.get(position).get("userName") + "      " + StringUtil.valueOf(dataList.get(position).get("dept")));
        if (dataList.get(position).containsKey("followStyle"))
            ((TextView) convertView.findViewById(R.id.type)).setText(dataList.get(position).get("followStyle").toString());

        return convertView;
    }


    public interface OnCallListener {
        void onCall(String tel);
    }

    OnCallListener callListener;

    public void setOnCallListener(OnCallListener listener) {
        this.callListener = listener;
    }
}
